package per.ethan.javase.designparttern.singleton;

import java.lang.reflect.Constructor;

/**
 * @author Ethan
 * @date 2020/06/01 15:13
 * 饿汉模式
 */
public class HungrySingleton {

    private static final HungrySingleton HUNGRY_SINGLETON = new HungrySingleton();

    private HungrySingleton() {

    }

    public static HungrySingleton getInstance() {
        return HUNGRY_SINGLETON;
    }

    // 反射可以破坏单例模式
    public static void main(String[] args) {
        HungrySingleton instance = HungrySingleton.getInstance();
        try {
            Constructor<HungrySingleton> constructor = HungrySingleton.class.getDeclaredConstructor();
            HungrySingleton instance2 = constructor.newInstance();
            System.out.println(instance == instance2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
